package com.wenhui.schoolyardinfo.firstpage.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nex3z.flowlayout.FlowLayout;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.FlowLayoutHotSearchOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.FlowLayoutHotSearchOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.FlowLayoutOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.FlowLayoutOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.firstpage.bean.SchoolChoiceBean;
import com.wenhui.schoolyardinfo.manager.IsEnglishLetterManager;
import com.wenhui.schoolyardinfo.manager.LogManager;
import com.wenhui.schoolyardinfo.manager.ScreenManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/20 10:33
 * introduce :
 */

public class SchoolChoiceAdapter extends RecyclerView.Adapter {

    private static final String TAG = "SchoolChoiceAdapter";
    private Context context;
    private List<String> historicRecordsList = new ArrayList<>();
    private List<String> hotSchoolList = new ArrayList<>();
    private List<SchoolChoiceBean> schoolChoiceBeanList = new ArrayList<>();
    private List<TextView> historicRecordsTextViewList = new ArrayList<>();
    private List<TextView> hotSchoolTextViewList = new ArrayList<>();
    private int historicRecordsPosition;
    private int hotSchoolPosition;
    private LinearLayoutManager layoutManager;
    private boolean stateChanged;
    private static final int HEADER_FIRST_VIEW_TYPE = 1;
    private static final int HEADER_SECOND_VIEW_TYPE = 2;
    private static final int CONTENT_VIEW_TYPE = 3;

    public SchoolChoiceAdapter(Context context) {
        this.context = context;
        stateChanged = false;
    }

    public SchoolChoiceAdapter(Context context, List<String> historicRecordsList, List<String> hotSchoolList,
                               List<SchoolChoiceBean> schoolChoiceBeanList) {
        this.context = context;
        this.historicRecordsList = historicRecordsList;
        this.hotSchoolList = hotSchoolList;
        this.schoolChoiceBeanList = schoolChoiceBeanList;
        stateChanged = false;
    }

    public void clearData() {
        historicRecordsList.clear();
        hotSchoolList.clear();
        schoolChoiceBeanList.clear();
        historicRecordsTextViewList.clear();
        hotSchoolTextViewList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<String> historicRecordsList, List<String> hotSchoolList, int historicRecordsPosition, int hotSchoolPosition, List<SchoolChoiceBean> schoolChoiceBeanList) {
        this.historicRecordsList.addAll(historicRecordsList);
        this.hotSchoolList.addAll(hotSchoolList);
        this.historicRecordsPosition = historicRecordsPosition;
        this.hotSchoolPosition = hotSchoolPosition;
        this.schoolChoiceBeanList.addAll(schoolChoiceBeanList);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case HEADER_FIRST_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_school_choice_header_first, parent, false);
                return new HeaderFirstHolder(view);
            case HEADER_SECOND_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_school_choice_header_second, parent, false);
                return new HeaderSecondHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_school_choice_content, parent, false);
                return new ContentHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderFirstHolder) {
            HeaderFirstHolder headerFirstHolder = (HeaderFirstHolder) holder;
            headerFirstHolder.flowHistory.removeAllViews();

            if (historicRecordsList != null && historicRecordsList.size() > 0) {
                headerFirstHolder.layoutHistory.setVisibility(View.VISIBLE);
                for (int i = 0; i < historicRecordsList.size(); i++) {
                    TextView textView = buildHistoricRecordsLabel(historicRecordsList.get(i), i, historicRecordsPosition);
                    headerFirstHolder.flowHistory.addView(textView);
                    historicRecordsTextViewList.add(textView);
                }
            } else {
                headerFirstHolder.layoutHistory.setVisibility(View.GONE);
            }
        } else if (holder instanceof HeaderSecondHolder) {
            HeaderSecondHolder headerSecondHolder = (HeaderSecondHolder) holder;
            headerSecondHolder.flowHotSchools.removeAllViews();

            if (hotSchoolList != null && hotSchoolList.size() > 0) {
                headerSecondHolder.layoutHot.setVisibility(View.VISIBLE);
                for (int i = 0; i < hotSchoolList.size(); i++) {
                    TextView textView = buildHotSchoolLabel(hotSchoolList.get(i), i, hotSchoolPosition);
                    headerSecondHolder.flowHotSchools.addView(textView);
                    hotSchoolTextViewList.add(textView);
                }
            } else {
                headerSecondHolder.layoutHot.setVisibility(View.GONE);
            }
        } else if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            if (IsEnglishLetterManager.isEnglishLetter(schoolChoiceBeanList.get(position - 2).getName())) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) contentHolder.layoutSchool.getLayoutParams();
                layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                layoutParams.height = ScreenManager.dipTopx(context, 31);
                contentHolder.layoutSchool.setLayoutParams(layoutParams);
            } else {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) contentHolder.layoutSchool.getLayoutParams();
                layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                layoutParams.height = ScreenManager.dipTopx(context, 44);
                contentHolder.layoutSchool.setLayoutParams(layoutParams);
                if (schoolChoiceBeanList.get(position - 2).isChecked()) {
                    contentHolder.tevSchool.setTextColor(context.getResources().getColor(R.color.colorBlueA));
                } else {
                    contentHolder.tevSchool.setTextColor(context.getResources().getColor(R.color.colorBlackA));
                }
            }
            contentHolder.tevSchool.setText(schoolChoiceBeanList.get(position - 2).getName());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_FIRST_VIEW_TYPE;
        } else if (position == 1) {
            return HEADER_SECOND_VIEW_TYPE;
        } else {
            return CONTENT_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return schoolChoiceBeanList.size() + 2;
    }

    public void setLayoutManager(LinearLayoutManager manager) {
        this.layoutManager = manager;
    }

    /**
     * 滚动RecyclerView到索引位置
     *
     * @param index
     */
    public void scrollToSection(String index) {
        LogManager.i(TAG, "scrollToSection*****" + index);
        if (schoolChoiceBeanList != null && schoolChoiceBeanList.size() > 0) {
            if (index != null && !"".equals(index)) {
                int size = schoolChoiceBeanList.size();
                for (int i = 0; i < size; i++) {
                    if ((index.substring(0, 1)).equals(schoolChoiceBeanList.get(i).getSection().substring(0, 1))) {
                        if (layoutManager != null) {
                            LogManager.i(TAG, "scrollToSectionAA*****" + index);
                            layoutManager.scrollToPositionWithOffset(i + 2, 0);
                            if ("历".equals(index.substring(0, 1))) {
                                //防止滚动时进行刷新
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (stateChanged) notifyItemChanged(0);
                                    }
                                }, 1000);
                            }
                            return;
                        }
                    } else {
                        if ("历史".equals(index)) {
                            LogManager.i(TAG, "scrollToSection历史*****" + index);
                            layoutManager.scrollToPositionWithOffset(0, 0);
                            if ("历".equals(index.substring(0, 1))) {
                                //防止滚动时进行刷新
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (stateChanged) notifyItemChanged(0);
                                    }
                                }, 1000);
                            }
                            return;
                        } else if ("热门".equals(index)) {
                            LogManager.i(TAG, "scrollToSection热门*****" + index);
                            layoutManager.scrollToPositionWithOffset(1, 0);
                            if ("热".equals(index.substring(0, 1))) {
                                //防止滚动时进行刷新
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (stateChanged) notifyItemChanged(0);
                                    }
                                }, 1000);
                            }
                            return;
                        } else {
                            LogManager.i(TAG, "scrollToSection热门CC*****" + index);
                        }
                    }
                }
            }
        }
    }

    private TextView buildHistoricRecordsLabel(String text, final int position, int historicRecordsPosition) {
        final TextView textView = new TextView(context);
        setFlowLayoutOnItemViewClickListener(new FlowLayoutOnItemViewClickListener() {
            @Override
            public void onItemFlowLayoutClickListener(int position, View view) {
                for (int i = 0; i < hotSchoolList.size(); i++) {
                    hotSchoolTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                }

                for (int i = 0; i < historicRecordsList.size(); i++) {
                    if (i == position) {
                        historicRecordsTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_blue_a_stroke_1_blue_b);
//                        showToast("点击了子项");

                        flowLayoutOnItemItemViewClickListener.onItemItemFlowLayoutClickListener(position, view);
                        return;
                    } else {
                        historicRecordsTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                    }
                }
            }
        });
        textView.setText(text);
        textView.setTextColor(context.getResources().getColor(R.color.colorBlackA));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setPadding(ScreenManager.dipTopx(context, 9),
                ScreenManager.dipTopx(context, 7),
                ScreenManager.dipTopx(context, 9),
                ScreenManager.dipTopx(context, 7));
        if (position == historicRecordsPosition) {
            textView.setBackgroundResource(R.drawable.bg_corners_2_blue_a_stroke_1_blue_b);
        } else {
            textView.setBackgroundResource(R.drawable.bg_corners_2_gray_c);
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                flowLayoutOnItemViewClickListener.onItemFlowLayoutClickListener(position, view);
            }
        });

        return textView;
    }

    private TextView buildHotSchoolLabel(String text, final int position, int hotSchoolPosition) {
        final TextView textView = new TextView(context);
        setFlowLayoutHotSearchOnItemViewClickListener(new FlowLayoutHotSearchOnItemViewClickListener() {
            @Override
            public void onItemFlowLayoutHotSearchClickListener(int position, View view) {
                for (int i = 0; i < historicRecordsList.size(); i++) {
                    historicRecordsTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                }

                for (int i = 0; i < hotSchoolList.size(); i++) {
                    if (i == position) {
                        hotSchoolTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_blue_a_stroke_1_blue_b);
//                        showToast("点击了子项");
                        flowLayoutHotSearchOnItemItemViewClickListener.onItemItemFlowLayoutHotSearchClickListener(position, view);
                        return;
                    } else {
                        hotSchoolTextViewList.get(i).setBackgroundResource(R.drawable.bg_corners_2_gray_c);
                    }
                }
            }
        });
        textView.setText(text);
        textView.setTextColor(context.getResources().getColor(R.color.colorBlackA));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setPadding(ScreenManager.dipTopx(context, 9),
                ScreenManager.dipTopx(context, 7),
                ScreenManager.dipTopx(context, 9),
                ScreenManager.dipTopx(context, 7));
        if (position == hotSchoolPosition) {
            textView.setBackgroundResource(R.drawable.bg_corners_2_blue_a_stroke_1_blue_b);
        } else {
            textView.setBackgroundResource(R.drawable.bg_corners_2_gray_c);
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                flowLayoutHotSearchOnItemViewClickListener.onItemFlowLayoutHotSearchClickListener(position, view);
            }
        });

        return textView;
    }

    class HeaderFirstHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_history)
        TextView tevHistory;
        @BindView(R.id.tev_clean_up)
        TextView tevCleanUp;
        @BindView(R.id.flow_history)
        FlowLayout flowHistory;
        @BindView(R.id.layout_history)
        LinearLayout layoutHistory;

        private HeaderFirstHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            tevCleanUp.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tev_clean_up:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    class HeaderSecondHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_hot_schools)
        TextView tevHotSchools;
        @BindView(R.id.tev_exchange)
        TextView tevExchange;
        @BindView(R.id.flow_hot_schools)
        FlowLayout flowHotSchools;
        @BindView(R.id.layout_hot)
        LinearLayout layoutHot;

        private HeaderSecondHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            tevExchange.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tev_exchange:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_school)
        TextView tevSchool;
        @BindView(R.id.layout_school)
        RelativeLayout layoutSchool;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutSchool.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_school:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }

    private FlowLayoutOnItemViewClickListener flowLayoutOnItemViewClickListener;

    public void setFlowLayoutOnItemViewClickListener(FlowLayoutOnItemViewClickListener flowLayoutOnItemViewClickListener) {
        this.flowLayoutOnItemViewClickListener = flowLayoutOnItemViewClickListener;
    }

    private FlowLayoutHotSearchOnItemViewClickListener flowLayoutHotSearchOnItemViewClickListener;

    public void setFlowLayoutHotSearchOnItemViewClickListener(FlowLayoutHotSearchOnItemViewClickListener flowLayoutHotSearchOnItemViewClickListener) {
        this.flowLayoutHotSearchOnItemViewClickListener = flowLayoutHotSearchOnItemViewClickListener;
    }

    private FlowLayoutOnItemItemViewClickListener flowLayoutOnItemItemViewClickListener;

    public void setFlowLayoutOnItemItemViewClickListener(FlowLayoutOnItemItemViewClickListener flowLayoutOnItemItemViewClickListener) {
        this.flowLayoutOnItemItemViewClickListener = flowLayoutOnItemItemViewClickListener;
    }

    private FlowLayoutHotSearchOnItemItemViewClickListener flowLayoutHotSearchOnItemItemViewClickListener;

    public void setFlowLayoutHotSearchOnItemItemViewClickListener(FlowLayoutHotSearchOnItemItemViewClickListener flowLayoutHotSearchOnItemItemViewClickListener) {
        this.flowLayoutHotSearchOnItemItemViewClickListener = flowLayoutHotSearchOnItemItemViewClickListener;
    }
}
